package org.roulette.core;

import org.roulette.utils.BetConstant;
import org.roulette.utils.NumberUtils;
import org.roulette.utils.WinUtils;
import java.util.Collection;

/**
 *
 * @author hudimar
 */
public class Bet implements OneRoundListener{

	private BetConstant bet;
	private int betAmount = 0;
	private int winAmount = 0;
	private BetStatus status = BetStatus.none;
	private boolean finished = false;

	private WinListener better;

	enum BetStatus{
		win, loss, invalid, none;
	}

	public Bet(BetConstant bet, int betAmount, WinListener better){
		this.bet = bet;
		this.betAmount = betAmount;
		this.better = better;
	}

    public boolean isFinished(){
        return finished;
    }
    public boolean isWin(){
        return status == BetStatus.win;
    }
    public boolean isLoss(){
        return status == BetStatus.loss;
    }
    public boolean isInvalid(){
        return status == BetStatus.invalid;
    }
    public void setInvalid(String message){
        status = BetStatus.invalid;
		finished = true;
    }
	public int getWinAmount(){
		return winAmount;
	}
	public int getBetAmount(){
		return betAmount;
	}

    public void passResult(int number) {
		if(finished)
			throw new IllegalStateException("Bet is finished.");
        Collection<BetConstant> win = NumberUtils.getPossibilities(number);
		if(win.contains(bet))
			status = BetStatus.win;
		else
			status = BetStatus.loss;
		System.out.println("isWin: "+isWin());
		if(isWin()){
			winAmount = WinUtils.calculateWin(bet, betAmount);
			better.win(winAmount);
		}
		finished = true;
    }
}
